﻿// ===============================================================================
// DotNet.Platform 开发框架 2016 版权所有
// ===============================================================================

using System.Collections.Generic;
using System.Linq;
using DotNet.JCore.Entity;
using DotNet.Collections;
using DotNet.Extensions;
using DotNet.Utility;
using DotNet.Data;
using DotNet.Helper;
using System;

namespace DotNet.JCore.Service
{
    /// <summary>
    /// 登录日志服务
    /// </summary>
    public class AccountLogService
    {
        private JCoreRepository<AccountLog> repos = new JCoreRepository<AccountLog>();

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="emp">员工实体</param>
        /// <param name="opType">操作类型(0登录1退出)</param>
        public BoolMessage Insert(Employee emp, int opType)
        {
            var entity = new AccountLog();
            entity.Id = StringHelper.Guid();
            entity.Name = emp.Name;
            entity.Phone = emp.Phone;
            entity.Dept = emp.Dept;
            entity.PointId = emp.PointId;
            entity.PointName = emp.PointName;
            entity.CreateTime = DateTime.Now;
            entity.OpType = opType;

            repos.Insert(entity);
            return BoolMessage.True;
        }

        /// <summary>
        /// 获取对象分页集合
        /// </summary>
        /// <param name="pageCondition">分页对象</param>
        /// <param name="name">名称关键字</param>
        /// <param name="pointId">检查点</param>
        public PageList<AccountLog> GetPageList(PaginationCondition pageCondition,
            string name, string pointId, string CreateId, DateTime? startDate, DateTime? endDate)
        {
            pageCondition.SetDefaultOrder(nameof(AccountLog.CreateTime));
            var query = repos.PageQuery(pageCondition);
            SetQuery(query, name, pointId, startDate, endDate, CreateId);
            return repos.Page(query);
        }

        /// <summary>
        /// 设置查询条件
        /// </summary>
        /// <param name="query">查询对象</param>
        /// <param name="search">数据对象</param>
        public void SetQuery(SQLQuery<AccountLog> query, string name, string pointId,
            DateTime? startDate, DateTime? endDate,string CreateId)
        {
            if (name.IsNotEmpty())
            {
                name = name.Trim();
                query.Where(p => p.Phone.Contains(name)
                 || p.Dept.Contains(name)
                );
            }
            if (pointId.IsNotEmpty())
            {
                pointId = pointId.Trim();
                query.Where(p => p.PointId == pointId);
            }
            if (CreateId.IsNotEmpty()&& CreateId!="0")
            {
                CreateId = CreateId.Trim();
                query.Where(p => p.Name == CreateId);
            }
            if (pointId.IsNotEmpty())
            {
                pointId = pointId.Trim();
                query.Where(p => p.PointId == pointId);
            }
            if (startDate.HasValue)
            {
                var startDateDt = startDate.ToDateTime();
                query.Where(p => p.CreateTime >= startDateDt);
            }

            if (endDate.HasValue)
            {
                var endDateDt = endDate.ToDateTime().AddDays(1);
                query.Where(p => p.CreateTime <= endDateDt);
            }
        }
    }
}
